package com.house.system.service.impl;

import java.util.List;

import com.house.common.core.domain.entity.DictData;
import com.house.common.utils.DateUtils;
import com.house.common.utils.DictUtils;
import com.house.system.mapper.DictDataMapper;
import com.house.system.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 字典数据Service业务层处理
 * 
 * @author tc
 * @date 2022-02-04
 */
@Service
public class DictDataServiceImpl implements IDictDataService
{
    @Autowired
    private DictDataMapper dictDataMapper;

    /**
     * 查询字典数据
     * 
     * @param dictCode 字典数据主键
     * @return 字典数据
     */
    @Override
    public DictData selectDictDataByDictCode(Long dictCode)
    {
        return dictDataMapper.selectDictDataByDictCode(dictCode);
    }

    /**
     * 查询字典数据列表
     * 
     * @param dictData 字典数据
     * @return 字典数据
     */
    @Override
    public List<DictData> selectDictDataList(DictData dictData)
    {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 新增字典数据
     * 
     * @param data 字典数据
     * @return 结果
     */
    @Override
    public int insertDictData(DictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<DictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改字典数据
     * 
     * @param dictData 字典数据
     * @return 结果
     */
    @Override
    public int updateDictData(DictData dictData)
    {
        int row = dictDataMapper.updateDictData(dictData);
        if (row > 0)
        {
            List<DictData> dictDatas = dictDataMapper.selectDictDataByType(dictData.getDictType());
            DictUtils.setDictCache(dictData.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 批量删除字典数据
     * 
     * @param dictCodes 需要删除的字典数据主键
     */
    @Override
    public void deleteDictDataByDictCodes(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            DictData data = selectDictDataByDictCode(dictCode);
            dictDataMapper.deleteDictDataByDictCode(dictCode);
            List<DictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }


}
